package com.atguigu.day05;

import com.atguigu.bean.WaterSensor;
import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

public class Flink04_TimeWindow_TumblingWindow_ProcessFun {
    public static void main(String[] args) throws Exception {
        //1.获取流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        //2.从端口读取数据
        DataStreamSource<String> streamSource = env.socketTextStream("localhost", 9999);

        //3.将数据转为WaterSensor
        SingleOutputStreamOperator<WaterSensor> map = streamSource.map(new MapFunction<String, WaterSensor>() {
            @Override
            public WaterSensor map(String value) throws Exception {
                String[] split = value.split(",");
                return new WaterSensor(split[0], Long.parseLong(split[1]), Integer.parseInt(split[2]));
            }
        });

        //4.将相同id的数据聚合到一块
        KeyedStream<WaterSensor, Tuple> keyedStream = map.keyBy("id");

        // 5.开启一个基于时间的滚动窗口 窗口大小为 5S
        WindowedStream<WaterSensor, Tuple, TimeWindow> window = keyedStream.window(TumblingProcessingTimeWindows.of(Time.seconds(5)));

       //TODO 6.使用全窗口函数
        window
                .process(new ProcessWindowFunction<WaterSensor, Integer, Tuple, TimeWindow>() {

            private Integer sumVc = 0;

            @Override
            public void process(Tuple tuple, Context context, Iterable<WaterSensor> elements, Collector<Integer> out) throws Exception {
                System.out.println("process"+tuple.toString());
                for (WaterSensor element : elements) {
                    sumVc += element.getVc();
                }
                out.collect(sumVc);
            }
        })

           /* .apply(new WindowFunction<WaterSensor, String, Tuple, TimeWindow>() {
                private Integer sumVc = 0;
                @Override
                public void apply(Tuple tuple, TimeWindow window, Iterable<WaterSensor> input, Collector<String> out) throws Exception {
                    System.out.println("apply");
                    for (WaterSensor waterSensor : input) {
                        sumVc += waterSensor.getVc();
                    }
                    out.collect(sumVc+"");
                }
            })*/

                .print();
        env.execute();

    }
}
